﻿using System.Collections.Generic;
using System.Data;

namespace S2_Xxxx_XxxNetApi
{
    public class BaseRepository
    {
        /// <summary>
        /// 数据访问对象，此处暂时写为常量，也可以改为netcore自带的注入
        /// </summary>
        private static IDapperHelper dapperHelper = GetDapperHelper;

        private static IDapperHelper GetDapperHelper
        {
            get
            {
                IDapperHelper _dapperHelper;
                string DbType = AppSettings.DbType;
                switch (DbType)
                {
                    case "MySql":
                        _dapperHelper = new MySqlDapperHelper();
                        break;
                    case "Oracle":
                        _dapperHelper = new OracleDapperHelper();
                        break;
                    case "Sqlserver":
                        _dapperHelper = new SqlServerDapperHelper();
                        break;
                    default:
                        _dapperHelper = new MySqlDapperHelper();
                        break;
                }
                return _dapperHelper;
            }
        }

        public static int ExecuteSql(string sql)
        {
            return dapperHelper.ExecuteSql(sql);
        }

        public static DataTable GetByPage(string sql, int pageSize, int pageNumber, out int pageCount)
        {
            DataTable dt = dapperHelper.GetByPage(sql, pageSize, pageNumber, out pageCount);
            return dt;
        }

        public static DataTable GetBySql(string sql)
        {
            return dapperHelper.GetBySql(sql);
        }
    }

    public class BaseRepository<T>
    {
        //描述
        //这里使用了“中介模式”，将数据库访问与缓存访问在此处做集中处理，规避了上述两者存在的耦合性
        //对外统一暴露为orm形态
        //只有增、删、改才涉及到缓存处理，默认为同步处理缓存，提供手动取消同步缓存的控制
        //开发者所有对数据的访问，均只需要通过本对象即可
        //

        /// <summary>
        /// 数据访问对象，此处暂时写为常量，也可以改为netcore自带的注入
        /// </summary>
        private static IDapperHelper<T> dapperHelper = GetDapperHelper;

        private static IDapperHelper<T> GetDapperHelper
        {
            get
            {
                IDapperHelper<T> _dapperHelper;
                string DbType = AppSettings.DbType;
                switch (DbType)
                {
                    case "MySql":
                        _dapperHelper = new MySqlDapperHelper<T>();
                        break;
                    case "Oracle":
                        _dapperHelper = new OracleDapperHelper<T>();
                        break;
                    case "Sqlserver":
                        _dapperHelper = new SqlServerDapperHelper<T>();
                        break;
                    default:
                        _dapperHelper = new MySqlDapperHelper<T>();
                        break;
                }
                return _dapperHelper;
            }
        }

        /// <summary>
        /// 缓存访问对象，此处暂时写为常量，也可以改为netcore自带的注入
        /// </summary>
        private static ICacheHelper<T> cacheHelper = new CacheHelper<T>();

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public static List<T> Get()
        {
            return dapperHelper.Get();
        }

        /// <summary>
        /// 根据条件获取数据
        /// </summary>
        /// <param name="where">条件(不需要前面带where字符串)</param>
        /// <returns></returns>
        public static List<T> Get(string where)
        {
            return dapperHelper.Get(where);
        }

        /// <summary>
        /// 排序获取数据
        /// </summary>
        /// <param name="where">条件(不需要前面带where字符串)</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="orderByType">排序类型：asc/desc</param>
        /// <returns></returns>
        public static List<T> Get(string where, string orderBy, string orderByType)
        {
            return dapperHelper.Get(where, orderBy, orderByType);
        }

        /// <summary>
        /// 获取前几行数据
        /// </summary>
        /// <param name="where">条件(不需要前面带where字符串)</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="orderByType">排序类型：asc/desc</param>
        /// <param name="topNumber">前多少条数据</param>
        /// <returns></returns>
        public static List<T> Get(string where, string orderBy, string orderByType, int topNumber)
        {
            return dapperHelper.Get(where, orderBy, orderByType, topNumber);
        }

        /// <summary>
        /// 获取第一行数据
        /// </summary>
        /// <param name="where">条件(不需要前面带where字符串)</param>
        /// <returns></returns>
        public static T GetFirst(string where)
        {
            return dapperHelper.GetFirst(where);
        }

        /// <summary>
        /// 获取第一行数据
        /// </summary>
        /// <param name="where">条件(不需要前面带where字符串)</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="orderByType">排序类型：asc/desc</param>
        /// <returns></returns>
        public static T GetFirst(string where, string orderBy, string orderByType)
        {
            return dapperHelper.GetFirst(where, orderBy, orderByType);
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="where">条件(不需要前面带where字符串)</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="orderByType">排序类型：asc/desc</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageNumber">页码</param>
        /// <param name="pageCount">总页数</param>
        /// <returns></returns>
        public static List<T> GetByPage(string where, string orderBy, string orderByType, int pageSize, int pageNumber, out int pageCount)
        {
            return dapperHelper.GetByPage(where, orderBy, orderByType, pageSize, pageNumber, out pageCount);
        }

        /// <summary>
        /// 通过sql数据获取数据
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public DataTable GetBySql(string sql)
        {
            return dapperHelper.GetBySql(sql);
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public int ExecuteSql(string sql)
        {
            return dapperHelper.ExecuteSql(sql);
        }

        /// <summary>
        /// 判断数据是否存在
        /// </summary>
        /// <param name="where">条件(不需要前面带where字符串)</param>
        /// <returns></returns>
        public static bool Exist(string where)
        {
            return dapperHelper.Exist(where);
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="model">数据对象</param>
        /// <returns></returns>
        public static bool Insert(T model, bool insertCache = true)
        {
            bool bl = dapperHelper.Insert(model);

            if (insertCache && bl)
            {
                cacheHelper.Insert(model);
            }

            return bl;
        }

        /// <summary>
        /// 批量新增数据
        /// </summary>
        /// <param name="listModel">数据对象集合</param>
        /// <returns></returns>
        public static bool Insert(List<T> listModel, bool insertCache = true)
        {
            bool bl = dapperHelper.Insert(listModel);
            if (insertCache && bl)
            {
                cacheHelper.Insert(listModel);
            }
            return bl;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="model">数据对象</param>
        /// <returns></returns>

        public static bool Edit(T model, bool editCache = true)
        {
            bool bl = dapperHelper.Edit(model);
            if (editCache && bl)
            {
                cacheHelper.Edit(model);
            }
            return bl;
        }

        /// <summary>
        /// 批量修改数据
        /// </summary>
        /// <param name="listModel">数据对象集合</param>
        /// <returns></returns>
        public static bool Edit(List<T> listModel, bool editCache = true)
        {
            bool bl = dapperHelper.Edit(listModel);
            if (editCache && bl)
            {
                cacheHelper.Edit(listModel);
            }
            return bl;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="model">数据对象</param>
        /// <returns></returns>
        public static bool Delete(T model, bool deleteCache = true)
        {
            bool bl = dapperHelper.Delete(model);
            if (deleteCache && bl)
            {
                cacheHelper.Delete(model);
            }
            return bl;
        }

        /// <summary>
        /// 批量删除对象
        /// </summary>
        /// <param name="listModel">数据对象集合</param>
        /// <returns></returns>
        public static bool Delete(List<T> listModel, bool deleteCache = true)
        {
            bool bl = dapperHelper.Delete(listModel);
            if (deleteCache && bl)
            {
                cacheHelper.Delete(listModel);
            }
            return bl;
        }


    }
}
